<?php
namespace lib;
class Report {
    
    public static function __callstatic($a, $b) {
        var_dump('no method');
    }
    
    
    //设备登录
    public static function tcpLogin($params) {
        try {
            
            $localRedis = new LocalRedis();
            $localRedis->lpush('login-list', $params['deviceId']);
            $localRedis->close();
            
        } catch (\Exception $e) {
        }
        return true;
    }
    
    
    //设备心跳
    public static function tcpGpsHeatbeat($params) {
        try {
            $localRedis = new LocalRedis();
            $localRedis->lpush('heart-list', ['deviceId' => $params['deviceId'], 'lock_status' => $params['lockStatus'], 'signal' => $params['onOff2'], 'ports' => $params['params']]);
            $localRedis->close();
        } catch (\Exception $e) {
        }
        
        return true;
    }
    
    //充电结果通知的
    public static function chargeResult($params) {
        $sim = $params['deviceId'];
        $portNo = $params['socketNo'];
        
        $time = time();
        
        $orderId = 0;
        $deviceId = 0;
        $orderIdKey = $sim . ':' . $portNo;
        
        //先从本地redis取
        try {
            $localRedis = new LocalRedis();
            $orderId = $localRedis->get($orderIdKey);
            $deviceId = $localRedis->get($sim);
            $localRedis->close();
        } catch (\Exception $e) {
            
        }
        
        if(empty($orderId)) { //从redis库取
            try {
                $redis = new Redis();
                $res = $redis->connect();
                $orderId = $redis->get($orderIdKey);
                $redis->close();
            } catch (\Exception $e) {
            }
        }
        
        $mysql = new Mysql();
        
        if(empty($orderId) || empty($deviceId)){
            $deviceId = $mysql->value('device', "sim_iccid='$sim'", 'device_id');
            $orderId = $mysql->value('device_port', "device_id= {$deviceId} and port_no = {$portNo}", 'current_order_id');
        }
        
        if($params['servRes'] == 1){ //充电成功
            $mysql->update('charge_order', "order_id = {$orderId}", ['status' => 1, 'start_time' => $time]);
            $mysql->update('device_port', "current_order_id = {$orderId}", ['status' => 1]);
        }else{
            $rs = $mysql->update('charge_order', "order_id = {$orderId}", ['status' => 3]);
            //请求退款
            $request = new Request();
            $request->post('chargeRefund', ['id' => $orderId]);
            $request->close();
        }
        
        $mysql->update('device', 'device_id = ' . $deviceId, ['heart_time' => $time]);
        
        $mysql->close();
        return true;
    }
    
    //增加充电时间通知
    public static function addChargeResult($params) {
        $sim = $params['deviceId'];
        $portNo = $params['socketNo'];
        
        
        $chargeOrderId = 0;
        $chargeOrderIdKey = $sim . ':' . $portNo;
        $orderId = 0;
        $orderIdKey = 'extend:' . $sim . ':' . $portNo;
        
        try {//从本地取
            $localRedis = new LocalRedis();
            $chargeOrderId = $localRedis->get($chargeOrderIdKey);
            $orderId = $localRedis->get($orderIdKey);
            $localRedis->close();
        } catch (\Exception $e) {
        }
        
        if(empty($chargeOrderId) || empty($orderId)) { //从远程取
            try {
                $redis = new Redis();
                $res = $redis->connect();
                $chargeOrderId = $redis->get($chargeOrderIdKey);
                $orderId = $redis->get($orderIdKey);
                $redis->close();
            } catch (\Exception $e) {
            }
        }
        
        $mysql = new Mysql();
        
        if(empty($chargeOrderId) || empty($orderId)){
            $deviceId = $mysql->value('device', "sim_iccid='{$sim}'" , 'device_id');
            $chargeOrderId = $mysql->value('device_port', "device_id= {$deviceId} and port_no = {$portNo}", 'current_order_id');
            $chargeOrder = $mysql->queryOne("select order_id,extend_quantity,amount from extend_charge_order where charge_order_id = {$chargeOrderId} order by order_id desc limit 1");
            if(empty($chargeOrder)){
                $mysql->close();
                return true;
            }
            $extendQuantity = $chargeOrder['extend_quantity'];
            $extendAmount = $chargeOrder['amount'];
            $orderId = $chargeOrder['order_id'];
        }else{
            $order = $mysql->queryOne("select extend_quantity,amount from extend_charge_order where order_id = {$orderId} limit 1");
            $extendQuantity = $order['extend_quantity'];
            $extendAmount = $order['amount'];
        }
        
        if($params['servRes'] == 1){ //增加充电成功
            $mysql->update('extend_charge_order', "order_id = {$orderId}", ['status' => 1]);
            $mysql->query("update charge_order set charge_quantity = charge_quantity + {$extendQuantity},amount = amount + {$extendAmount} where order_id={$chargeOrderId}");
        }else{ //失败
            $mysql->update('extend_charge_order', "order_id = {$orderId}", ['status' => 2]);
            
            
            //请求退款
            $request = new Request();
            $request->post('extendChargeRefund', ['id' => $orderId]);
            $request->close();
        }
        $mysql->update('device', "sim_iccid = '{$sim}'", ['heart_time' => time()]);
        $mysql->close();
        return true;
    }
    
    
    //被动停止充电
    public static function finishChargeResult($params) {
        var_dump('finishChargeResult');
        var_dump(json_encode($params));
        
        return true;
    }
    
    
    //刷卡开始上报http://xxxx:7777/tboxmanager/updateCartUserInfo
    public static function updateCartUserInfo($params) {
        var_dump('updateCartUserInfo');
        var_dump(json_encode($params));
        
        return true;
    }
    
    //投币
    public static function updateTouBOrderInfo($params) {
        var_dump('updateTouBOrderInfo');
        var_dump(json_encode($params));
        
        return true;
    }
    
}









